import userApi from '@/api/user/index.js'
import addressApi from '@/api/mine/address.js'
import {clearUserCache} from '@/utils/index.js'
const state = {
	userInfo:{}
	
}
 
const mutations ={
	SET_USER_INFO(state,data){
		state.userInfo = data;
		try{
			uni.setStorageSync("userData",data);
		}catch(e){
			console.log(e)
		}
		
	},
	REMOVE_USER_INFO(state){
		state.userInfo = {};
		uni.removeStorageSync("userData");
	}
	
}

const actions = {
	doLogin({commit},data){
		return new Promise((resolve,reject)=>{
			let {username,password} = data;
			userApi.doLogin({
				username,
				password
			}).then(res=>{
				if(res.code == 'S'){
					// 清除旧信息
					clearUserCache()
					// 成功需要初始化用户信息
					commit('SET_USER_INFO',res.data)
					resolve();
				}else{
					reject('账号或密码错误!')
				}
			}).catch(err=>{
				reject(err)
			})
		})
	}
	
}

 

export default {
	namespaced: true,
	state,
	mutations,
	actions
}